home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / mozilla-firefox / include / xpcom / nsWeakReference.h < prev    next >
C/C++ Source or Header  |  2006-05-08  |  4KB  |  148 lines

  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * ***** BEGIN LICENSE BLOCK *****
  4.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  5.  *
  6.  * The contents of this file are subject to the Mozilla Public License Version
  7.  * 1.1 (the "License"); you may not use this file except in compliance with
  8.  * the License. You may obtain a copy of the License at
  9.  * http://www.mozilla.org/MPL/
  10.  *
  11.  * Software distributed under the License is distributed on an "AS IS" basis,
  12.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  13.  * for the specific language governing rights and limitations under the
  14.  * License.
  15.  *
  16.  * The Original Code is the Mozilla browser.
  17.  *
  18.  * The Initial Developer of the Original Code is
  19.  * Netscape Communications, Inc.
  20.  * Portions created by the Initial Developer are Copyright (C) 1999
  21.  * the Initial Developer. All Rights Reserved.
  22.  *
  23.  * Contributor(s):
  24.  *   Scott Collins <scc@netscape.com>
  25.  *
  26.  * Alternatively, the contents of this file may be used under the terms of
  27.  * either of the GNU General Public License Version 2 or later (the "GPL"),
  28.  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  29.  * in which case the provisions of the GPL or the LGPL are applicable instead
  30.  * of those above. If you wish to allow use of your version of this file only
  31.  * under the terms of either the GPL or the LGPL, and not to allow others to
  32.  * use your version of this file under the terms of the MPL, indicate your
  33.  * decision by deleting the provisions above and replace them with the notice
  34.  * and other provisions required by the GPL or the LGPL. If you do not delete
  35.  * the provisions above, a recipient may use your version of this file under
  36.  * the terms of any one of the MPL, the GPL or the LGPL.
  37.  *
  38.  * ***** END LICENSE BLOCK ***** */
  39.  
  40. #ifndef nsWeakReference_h__
  41. #define nsWeakReference_h__
  42.  
  43. // nsWeakReference.h
  44.  
  45. #include "nsIWeakReference.h"
  46. #include "nsIWeakReferenceUtils.h"
  47.  
  48. class nsWeakReference;
  49.  
  50. #undef  IMETHOD_VISIBILITY
  51. #define IMETHOD_VISIBILITY NS_VISIBILITY_DEFAULT
  52.  
  53. class NS_COM_GLUE nsSupportsWeakReference : public nsISupportsWeakReference
  54.   {
  55.     public:
  56.       nsSupportsWeakReference()
  57.           : mProxy(0)
  58.         {
  59.           // nothing else to do here
  60.         }
  61.  
  62.       NS_DECL_NSISUPPORTSWEAKREFERENCE
  63.  
  64.     protected:
  65.       inline ~nsSupportsWeakReference();
  66.  
  67.     private:
  68.       friend class nsWeakReference;
  69.  
  70.       void
  71.       NoticeProxyDestruction()
  72.           // ...called (only) by an |nsWeakReference| from _its_ dtor.
  73.         {
  74.           mProxy = 0;
  75.         }
  76.  
  77.       nsWeakReference* mProxy;
  78.  
  79.         protected:
  80.  
  81.             inline void ClearWeakReferences();
  82.             PRBool HasWeakReferences() const {return mProxy != 0;}
  83.   };
  84.  
  85. #undef  IMETHOD_VISIBILITY
  86. #define IMETHOD_VISIBILITY NS_VISIBILITY_HIDDEN
  87.  
  88. class NS_COM_GLUE nsWeakReference : public nsIWeakReference
  89.   {
  90.     public:
  91.     // nsISupports...
  92.       NS_DECL_ISUPPORTS
  93.  
  94.     // nsIWeakReference...
  95.       NS_DECL_NSIWEAKREFERENCE
  96.  
  97.     private:
  98.       friend class nsSupportsWeakReference;
  99.  
  100.       nsWeakReference( nsSupportsWeakReference* referent )
  101.           : mReferent(referent)
  102.           // ...I can only be constructed by an |nsSupportsWeakReference|
  103.         {
  104.           // nothing else to do here
  105.         }
  106.  
  107.       ~nsWeakReference()
  108.            // ...I will only be destroyed by calling |delete| myself.
  109.         {
  110.           if ( mReferent )
  111.             mReferent->NoticeProxyDestruction();
  112.         }
  113.  
  114.       void
  115.       NoticeReferentDestruction()
  116.           // ...called (only) by an |nsSupportsWeakReference| from _its_ dtor.
  117.         {
  118.           mReferent = 0;
  119.         }
  120.  
  121.       nsSupportsWeakReference*  mReferent;
  122.   };
  123.  
  124. inline
  125. void
  126. nsSupportsWeakReference::ClearWeakReferences()
  127.         /*
  128.             Usually being called from |nsSupportsWeakReference::~nsSupportsWeakReference|
  129.             will be good enough, but you may have a case where you need to call disconnect
  130.             your weak references in an outer destructor (to prevent some client holding a
  131.             weak reference from re-entering your destructor).
  132.         */
  133.     {
  134.         if ( mProxy )
  135.             {
  136.                 mProxy->NoticeReferentDestruction();
  137.                 mProxy = 0;
  138.             }
  139.     }
  140.  
  141. inline
  142. nsSupportsWeakReference::~nsSupportsWeakReference()
  143.   {
  144.       ClearWeakReferences();
  145.   }
  146.  
  147. #endif
  148.